
package com.jf.cloud.order.controller.platform;


import com.jf.cloud.api.vo.EsPageVO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.order.dto.OrderRefundSearchDTO;
import com.jf.cloud.common.order.vo.EsOrderRefundVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.order.constant.RefundApplyType;
import com.jf.cloud.order.model.OrderRefundAddr;
import com.jf.cloud.order.service.OrderRefundAddrService;
import com.jf.cloud.order.service.OrderRefundService;
import com.jf.cloud.order.vo.OrderRefundAddrVO;
import com.jf.cloud.order.vo.OrderRefundVO;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;


/**
 * @author zz
 */
@RestController("platformOrderRefundController")
@RequestMapping("/p/order_refund")
public class OrderRefundController {
    private final Logger LOGGER = LoggerFactory.getLogger(OrderRefundController.class);

    @Autowired
    private OrderRefundService orderRefundService;
    @Autowired
    private OrderRefundAddrService orderRefundAddrService;
    @Autowired
    private MapperFacade mapperFacade;

    /**
     * 查看我的退款订单列表
     */
    @GetMapping("/page")
    @Operation(summary = "我的退款订单列表" , description = "我的退款订单列表，显示数量时候")
    public ServerResponseEntity<EsPageVO<EsOrderRefundVO>> list(PageDTO page, @ParameterObject OrderRefundSearchDTO orderRefundPageDTO) {
        orderRefundPageDTO.setSysType(AuthUserContext.get().getSysType());
        EsPageVO<EsOrderRefundVO> pageList = orderRefundService.page(page, orderRefundPageDTO);
        return ServerResponseEntity.success(pageList);
    }

    /**
     * 通过id查询
     *
     * @param refundId id
     * @return 查询详细信息
     */
    @GetMapping("/info/{refundId}")
    public ServerResponseEntity<OrderRefundVO> getById(@ParameterObject @PathVariable("refundId") Long refundId) {
        OrderRefundVO orderRefund = orderRefundService.getDetailByRefundId(refundId);
        if (Objects.equals(orderRefund.getApplyType(), RefundApplyType.REFUND_AND_RETURNS.value())&& orderRefund.getHandelTime() !=null) {
            LOGGER.info("退货退款，查询退货地址");
            OrderRefundAddr orderRefundAddr = orderRefundAddrService.getByRefundId(refundId);
            orderRefund.setOrderRefundAddr(mapperFacade.map(orderRefundAddr, OrderRefundAddrVO.class));
        }
        return ServerResponseEntity.success(orderRefund);
    }


}
